//
//  ChangePassViewController.m
//  Utils
//
//  Created by enghou on 2018/5/1.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ChangePassViewController.h"
#import "LeftImageField.h"
#import "NetWorkerManager.h"
#import "LocalPersistent.h"
@interface ChangePassViewController ()
@property (weak, nonatomic) IBOutlet LeftImageField *oldPassField;
@property (weak, nonatomic) IBOutlet LeftImageField *nPassField;
@property (weak, nonatomic) IBOutlet LeftImageField *confirmPassField;
- (IBAction)submitAction:(id)sender;

@end

@implementation ChangePassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)submitAction:(id)sender {
    if ([self.oldPassField.text length] == 0) {
        [self alertSome:@"请输入旧密码!"];
        return;
    }
    if ([self.nPassField.text length] == 0) {
        [self alertSome:@"请输入新密码!"];
        return;
    }
    if ([self.confirmPassField.text length] == 0) {
        [self alertSome:@"请确认密码!"];
        return;
    }
    if ([self.confirmPassField.text isEqualToString:self.nPassField.text]) {
        [self beginLoading];
        [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/userMobileManage/updateUserPwd.action" parameter:@{@"oldPassword":self.oldPassField.text,@"currentPassword":self.nPassField.text,@"validatePassword":self.confirmPassField.text} completeBlock:^(NSDictionary *source) {
            [self endLoading];
            BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
            if (model.success) {
                NSString *account;
                NSString *pwd;
                [[LocalPersistent sharedInstance]latestLoginUser:&account passWord:&pwd];
                [[LocalPersistent sharedInstance]savePwdForAccount:account pwd:self.confirmPassField.text];
                [self pop];
            }else{
                [self alertSome:model.msg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
    }else{
        [self alertSome:@"确认密码不一致!"];
    }
}
@end
